//
//  ChannelView.m
//  UI_礼物说
//
//  Created by mac on 16/3/24.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ChannelView.h"
#import "ChannelCollectionViewCell.h"

@interface ChannelView () <UICollectionViewDelegate,UICollectionViewDataSource>

@end

@implementation ChannelView


-(void)dealloc
{
    [_arr release];
    [_collectionV release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews];
        
    }
    return self;
}

-(void)setArr:(NSArray *)arr
{
    if (_arr==nil) {
        [_arr release];
        _arr = [arr retain];
    }
    [self.collectionV reloadData];
}

-(void)createSubviews
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.minimumLineSpacing = 10;
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    CGFloat w = (SCREEN_WIDTH - 40)/3;
    flowLayout.itemSize = CGSizeMake(w-0.001 , 40);
    UICollectionView * collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.frame.size.height) collectionViewLayout:flowLayout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [self addSubview:collectionView];
    collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionV = collectionView;
    //注册
    [self.collectionV registerClass:[ChannelCollectionViewCell class] forCellWithReuseIdentifier:@"collectionVcell"];
 
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    //NSLog(@"%@",self.arr);
    return self.arr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ChannelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionVcell" forIndexPath:indexPath];
    
    cell.channelTitle.text = _arr[indexPath.row];
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate didSelectChannelViewItem:indexPath.item];
   
}


@end
